<?php

namespace app\api\service;

use think\facade\Db;

class GoodsService
{
    /**
     * 获取首页商品
     * */
    public static function getHomeGoodsList($store_id)
    {
        $goodsList = Db::name('goods')->field('id,goods_name,main_image,total_sale,market_price,is_subsidy,subsidy_price,sell_price,tags')
            ->where(['store_id' => $store_id, 'status' => 1, 'is_subsidy' => 0, 'delete_time' => null])
            ->withAttr('tags', function ($value) {
                return empty($value) ? [] : json_decode($value, true);
            })->withAttr('main_image', function ($value) {
            return request()->domain() . $value;
        })->order(['sort' => 'asc', 'total_sale' => 'desc', 'id' => 'desc'])
            ->limit(10)
            ->select();

        return $goodsList;
    }

    /**
     * 获取大额补贴商品
     * */
    public static function getSubsidyGoodsList($store_id)
    {
        $goodsList = Db::name('goods')
            ->field('id,goods_name,main_image,total_sale,sell_price,subsidy_price,tags')
            ->where(['store_id' => $store_id, 'status' => 1, 'is_subsidy' => 1, 'delete_time' => null])
            ->withAttr('tags', function ($value) {
                return empty($value) ? [] : json_decode($value, true);
            })->withAttr('main_image', function ($value) {
            return request()->domain() . $value;
        })->order(['sort' => 'asc', 'total_sale' => 'desc', 'id' => 'desc'])
            ->limit(5)
            ->select();

        return $goodsList;
    }

    /**
     * 获取会员专享商品
     * */
    public static function getMemberGoodsList($store_id)
    {
        $goodsList = Db::name('goods')
            ->field('id,goods_name,main_image,total_sale,market_price,sell_price,tags')
            ->where(['store_id' => $store_id, 'status' => 1, 'delete_time' => null])
            ->where('is_limit_member', 1)
            ->withAttr('tags', function ($value) {
                return empty($value) ? [] : json_decode($value, true);
            })->withAttr('main_image', function ($value) {
            return request()->domain() . $value;
        })->order(['sort' => 'asc', 'total_sale' => 'desc', 'id' => 'desc'])
            ->limit(5)
            ->select();

        return $goodsList;
    }

    /**
     *  获取商品列表
     * */
    public static function getGoodsList($param)
    {
        $query = Db::name('goods')
            ->alias('a')
            ->field('a.id,a.goods_name,a.main_image,a.total_sale,a.market_price,a.sell_price,a.is_subsidy,a.subsidy_price,a.tags')
            ->where(['a.store_id' => $param['store_id'], 'a.status' => 1, 'a.delete_time' => null])
            ->withAttr('tags', function ($value) {
                return empty($value) ? [] : json_decode($value, true);
            })->withAttr('main_image', function ($value) {
            return request()->domain() . $value;
        });

        if (!empty($param['cid'])) {
            $query->join('goods_mid_category b', 'a.id=b.goods_id', 'left')
                ->where('b.goods_category_id', $param['cid']);
        }

        if (!empty($param['is_limit_member'])) {
            $query->where('a.is_limit_member', 1);
        }

        if (!empty($param['is_subsidy'])) {
            $query->where('a.is_subsidy', 1);
        }

        if (!empty($param['keyword'])) {
            $query->whereLike('a.goods_name', "%{$param['keyword']}%");
        }

        if (!empty($param['is_hot'])) {
            $query->order('a.total_sale', 'desc');
        }

        $goodsList = $query->order('a.sort', 'asc')
            ->group('a.id')
            ->paginate(10);

        return $goodsList;
    }

    /**
     * 获取商品详情
     * */
    public static function findGoods($goods_id)
    {
        $goods = Db::name('goods')
            ->field('a.id,a.goods_name,a.main_image,a.spec_type,a.goods_images,a.goods_attr,a.market_price,a.sell_price,
                        a.is_limit_member,a.is_give_integral,a.is_free_task,a.is_subsidy,a.subsidy_price,a.total_sale,a.tags,a.stock,a.status,b.content')
            ->alias('a')
            ->join('goods_content b', 'a.id=b.goods_id')
            ->where('b.id', $goods_id)
            ->withAttr('tags', function ($value) {
                if (empty($value)) {
                    return [];
                }
                return json_decode($value, true);
            })
            ->withAttr('main_image', function ($value) {
                return request()->domain() . $value;
            })->withAttr('goods_attr', function ($value) {
            if (empty($value)) {
                return [];
            }
            return json_decode($value, true);
        })->withAttr('goods_images', function ($value) {
            $imgs = [];
            $arr = json_decode($value, true);
            foreach ($arr as $v) {
                $imgs[] = request()->domain() . $v;
            }
            return $imgs;
        })->find();

        $items = [];
        if ($goods['spec_type'] == 2) {
            $items = Db::name('goods_item')
                ->field('id,attr,image,market_price,sell_price,subsidy_price,stock')
                ->withAttr('image', function ($value) {
                    return request()->domain() . $value;
                })->where('goods_id', $goods['id'])
                ->select();
        }

        $goods['items'] = $items;

        return $goods;
    }

}
